package com.example.android.navigationdrawerexample;



import java.util.List;

import android.R.anim;
import android.R.style;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;

import com.phonebrand.dao.DB4CommandDao;
import com.phonebrand.global.GlobalApplication;
import com.phonebrand.model.FlagObjM;
import com.phonebrand.thread.RefreshData;

public class LoadPicScreen extends Activity {
	//private static final String LOG_TAG = "LoadPicScreen";
	public ProgressDialog progressDialog = null;
	private Handler myHandler;
	private int durationTime;
	public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			setTheme(style.Theme_NoTitleBar);
			setContentView(R.layout.splash);
			progressDialog = new ProgressDialog(this);  	
		   
			
			List<FlagObjM> listFlags = DB4CommandDao.getInstance().loadFlagObjM(((GlobalApplication)getApplication()).getDbPhone());
			
			if (listFlags == null || listFlags.size() == 0) {				
				FlagObjM objM = new FlagObjM(); 
				objM.setInitTerm("Y");
				DB4CommandDao.getInstance().storeFlag(objM,((GlobalApplication)getApplication()).getDbPhone());
				((GlobalApplication)getApplication()).getDbPhone().commit();
				durationTime = 20000;
			} else {
				durationTime = 2000;
			}	
			
			
		   if (!RefreshData.processLoading) {
	        	RefreshData refdata = new RefreshData(getApplicationContext());
	        	refdata.start();
	    	}
		   
		   myHandler = new Handler();
		   myHandler.postDelayed(new Runnable() {						   			   
			   public void run() {
				finish();
				Intent intent = new Intent(LoadPicScreen.this, MainActivity.class);
				startActivity(intent);
				overridePendingTransition(anim.fade_in, anim.fade_out);
			}
		}, durationTime);
		 
		   if (durationTime == 20000) {
			   showLoading();
		   }   
	}

	
	public void showLoading() {
		new Thread(new Runnable() {
		    public void run() { 
		    		LoadPicScreen.this.runOnUiThread(new Runnable() { 
		            public void run() {
		            	LoadPicScreen.this.progressDialog.setTitle(null);
		            	LoadPicScreen.this.progressDialog.setMessage("Please wait...");
		            	LoadPicScreen.this.progressDialog.setIndeterminate(true);
		            	LoadPicScreen.this.progressDialog.setCancelable(false);
		            	LoadPicScreen.this.progressDialog.show();
		            }
		        }); 
		    } 
		}).start(); 
//		thread.start(); 
	}
	
	public void hideLoading() {
		new Thread(new Runnable() {
		    public void run() { 
		    	LoadPicScreen.this.runOnUiThread(new Runnable() {
		            public void run() { 
		            	LoadPicScreen.this.progressDialog.dismiss();
		            } 
		        }); 
		    } 
		}).start(); 
	}	
	
 
	
	
	

}
